$(function () {
    obj.init();
});
var obj = new function () {
    return {
        id: null,
        form: null,
        city: null,
        district: null,
        init: function () {
            this.id = xhTool.getQuery('id');
            this.form = new xhForm($('#modify_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addSelectData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
            // //省份更改
            // $('#sProvince').change(function() {
            //     $this.funObj().changeProv($(this).val());
            // }).trigger('change');
            // //市更改
            // $('#sCity').change(function() {
            //     $this.funObj().changeCity($(this).val(),$('#sProvince').val());
            // }).trigger('change');
            //学校改变
            $('#iCUId').change(function() {
                $this.funObj().changeSchool($(this).val(),$('#iCUId').val());
            }).trigger('change');
        },
        funObj: function () {
            var $this = this;
            return {
                addSelectData: function () {
                    // var provincePro = $('#sProvince').select2({placeholder: '请选择', allowClear: true});
                    var schoolPro = $('#iCUId').select2({placeholder: '请选择', allowClear: true});
                    var modelPro = $('#sModel').select2({placeholder: '请选择', allowClear: true});
                    $('#iBoxType').select2({placeholder: '请选择'});
                    $('#sUserId').select2({placeholder: '请选择'});
                    // $('#sCity').select2({placeholder: '请选择', data: {}, allowClear: true});
                    // $('#sDistrict').select2({placeholder: '请选择', data: {}, allowClear: true});
                    if ($this.id) {//修改
                        xhExtra.ajaxRequest('device/getBoxInfoById', {id: $this.id}, {check: true, success: false}, function (boxInfo) {
                            // $this.city = boxInfo.sCity;
                            // $this.district = boxInfo.sDistrict;
                            $this.form.fill(boxInfo);
                            schoolPro.val([boxInfo.iCUId]).trigger('change');
                            modelPro.val([boxInfo.sModel]).trigger('change');
                            // if (boxInfo.sProvince) {
                            //     provincePro.val([boxInfo.sProvince]).trigger('change');
                            // }
                        });
                    }
                },
                changeSchool: function (_iCUId) {
                    xhExtra.ajaxRequest('device/getLocationById', {customerId:_iCUId}, {async:false}, function (result) {
                        $('#sLocation').val(result.locationInfo).trigger('change');
                    });
                },
                saveData:function () {
                    // var sMac = $('#sMac').val();
                    // if (!sMac) {
                    //     window.top.xhTop.showError('请填写mac信息');
                    //     xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                    //     return false;
                    // }
                    var params = {};
                    if ($this.id) {//修改
                        params.iId = $this.id;
                    }
                    $this.form.submit('device/saveBoxInfoData', params, function (res) {
                        if(res.code === 200){
                            window.top.xhTop.closeModal();
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        } else {
                            window.top.xhTop.showError(res.error);
                            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        }
                    },{check:false,success:true});
                },

                //以下废弃
                changeProv: function (provId) {
                    $('#sCity').html('');
                    $('#sDistrict').html('');
                    if (provId) {
                        xhExtra.ajaxRequest('device/getCityByProv', {provId:provId}, {async:false}, function (result) {
                            var cityPro = $('#sCity').select2({placeholder: '请选择', data: result, allowClear: true});
                            $('#sDistrict').select2({placeholder: '请选择', data: {}, allowClear: true});
                            if ($this.city) {
                                cityPro.val([$this.city]).trigger('change');
                            }
                        });
                    }
                },
                changeCity: function (cityId, provId) {
                    $('#sDistrict').html('');
                    if (cityId) {
                        xhExtra.ajaxRequest('device/getDistrictByCity', {provId:provId, cityId:cityId}, {async:false}, function (result) {
                            var districtPro = $('#sDistrict').select2({placeholder: '请选择', data: result, allowClear: true});
                            if ($this.district) {
                                districtPro.val([$this.district]).trigger('change');
                            }
                        });
                    }
                }
            }
        }
    }
};